using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SCI_actuator
{
    public partial class servo_info : Form
    {
        private menu_control frmain;
        private xmlrw sciset;
        private string pre_ID;
        public servo_info(menu_control fr)
        {
            InitializeComponent();
            this.frmain = fr;
            Initial();
        }
        private void Initial()
        {            
            sciset = new xmlrw();
            txtID.Text = pre_ID = sciset.Read_ServoID();
            txtBaudrate.Text = sciset.Read_Baudrate();
        }

        private void true_Btn_Click(object sender, EventArgs e)
        {
            string senddata = null;
            string sum_str = null;
            sciset.Write_ServoID(txtID.Text);
            sciset.Write_Baudrate(txtBaudrate.Text);
            byte[] bRx ;
            byte sum = 0;
            switch (txtBaudrate.Text)
            {
                case "2400":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "03" + constant.REG_BAUDRATE_L + "40");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "03" + constant.REG_BAUDRATE_L + "40" + sum_str;
                    break;
                case "4800":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "01" + constant.REG_BAUDRATE_L + "A0");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "01" + constant.REG_BAUDRATE_L + "A0" + sum_str;
                    break;
                case "9600":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "CF");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "CF" + sum_str;
                    break;
                case "14400":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "8A");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "8A" + sum_str;
                    break;
                case "19200":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "67");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "67" + sum_str;
                    break;
                case "38400":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "33");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "33" + sum_str;
                    break;
                case "57600":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "22");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "22" + sum_str;
                    break;
                case "115200":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "10");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "10" + sum_str;
                    break;
                case "230400":
                    bRx = this.frmain.HexStringToByteArray(pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "08");
                    sum = this.frmain.checksum(bRx);
                    if (sum >= 16)
                        sum_str = Convert.ToString(sum, 16);
                    else
                        sum_str = "0" + Convert.ToString(sum, 16);
                    senddata = "FF" + "FF" + pre_ID + "08" + "02" + constant.DEVICE_NUM + sciset.Read_ServoID()
                    + constant.REG_BAUDRATE_H + "00" + constant.REG_BAUDRATE_L + "08" + sum_str;
                    break;
            }
            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
            this.Hide();
            this.Dispose();
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }
    }
}